<?php
$adminuser = Users::getBackEndUser(SkillConfig::ADMIN_ID);
$imageName = (!empty($adminuser->photo)) ? getRequestbaseUrl() . getuploadFolderDir() . 'backend/' . SkillConfig::ADMIN_ID . '/profile/minithumb/' . $adminuser->photo : getWorkNoshopimage(SkillConfig::skill_myinbox_image);
?>
<section id="inbox-query">
    <div class="row">
        <div class="container mopo">
            <div class="col-lg-12 column12 padding-zero-left"><h1>User Messages</h1></div>          
            <div class="col-lg-3 col-md-3 col-sm-3 query-side column3 padding-zero-left">
                <ul class="unstyled review-tabs">
                    <li class="my-review active">
                        <a href="javascript:void(0)"data-toggle="tab" onclick="contacttoadmin()">
                            <img src="<?php echo $imageName; ?>" alt="">
                            <div class="name">Admin</div>
                        </a>
                    </li><!--single-review-->
                    <hr>  
                    <?php
                    if (!empty($allusers)) {
                        $i = 0;
                        foreach ($allusers as $alluser) {
                            $userDetails = Users::getFrontEndUser($alluser->from_users_id);
                            $imagePath = $alluser->from_users_id;
                            $imgsrc = ($userDetails->photo <> "") ? getRequestbaseUrl() . getuploadFolderDir() . $imagePath . '/profile/minithumb/' . $userDetails->photo : getWorkNoshopimage(SkillConfig::skill_myinbox_image);
                            $classCourseId = ($alluser->classes_id != '') ? $alluser->classes_id : $alluser->courses_id;
                            $classCourseType = ($alluser->classes_id != '') ? SkillConfig::class_name : SkillConfig::course_name;
                            ?>
                            <li class="my-review even">
                                <a href="javascript:void(0)" data-toggle="tab" onclick="conatcttoTrainer(<?php echo $alluser->from_users_id; ?>,<?php echo $alluser->to_user_id; ?>,<?php echo $classCourseId; ?>, '<?php echo $classCourseType; ?>')">
                                    <img src="<?php echo $imgsrc; ?>" alt="">
                                    <div class="name"><?php echo substr($userDetails->first_name, 0, 10); ?></div>
                                </a>
                            </li><!--single-review-->
                            <hr> 
                            <?php
                            $i++;
                        }
                    }
                    ?>
                </ul>       
            </div>
            <div class="col-lg-9 col-md-9 col-sm-9 column9">
                <!--tab content for inbox-query-->
                <div class="tab-content" id="replytomailconversion">

                    <?php
                    $dataByUser = ContactAdmin::getMessageCOntactToadmin();
                    // pr($dataByUser);
                    $this->renderPartial('//contactAdmin/contact-to-admin', array('dataByUser' => $dataByUser, 'adminuser' => $adminuser));
                    ?>
                </div><!--tab content for inbox-query-->
            </div>
        </div>
    </div>
</section>
<!--conversion section starts-->
<section id="conversion">
    <div class="row">
        <div class="container mopo">
            <hr>
            <div class="col-lg-12 padding-zero-left"><h1>conversion</h1></div>
            <div class="col-lg-3 col-md-3 col-sm-3 conver-side column3 padding-zero-left" id="myTab">
                <ul class="unstyled conver-tabs">
                    <?php
                    if (!empty($allconvertedQuery)) {
                        $i = 1;
                        foreach ($allconvertedQuery as $classname) {
                            if (!empty($classname->classes_id)) {
                                if ($i == 1) {
                                    $classId = $classname->classes_id;
                                }
                                $imageName = (ClassImage::getPrimaryImage($classname->classes_id) != '') ? getRequestbaseUrl() . getuploadFolderDir() . SkillConfig::getClassCourseImagePath(getLoginUserId()) . 'thumbs/' . ClassImage::getPrimaryImage($classname->classes_id) : getWorkNoshopimage(SkillConfig::skill_myinbox_image);
                                $classTitle = getClaCourseInfo('Classes', $classname->classes_id);
                                ?>
                                <li class="my-review <?php echo ($i == 1) ? 'active' : ''; ?>">
                                    <a href="javascript:void(0)"data-toggle="tab" onclick="convertedclassUser(<?php echo $classname->classes_id; ?>)">
                                        <img src="<?php echo $imageName; ?>" alt="">
                                        <div class="name"><?php echo substrclasscourname($classTitle); ?></div>
                                    </a>
                                </li><!--single-review-->
                                <hr> 
                                <?php
                            } elseif ($classname->courses_id != '') {
                                if ($i == 1) {
                                    $courseId = $classname->courses_id;
                                }
                                $imageName = (ClassImage::getCoursesPrimaryImage($classname->courses_id) != '') ? getRequestbaseUrl() . getuploadFolderDir() . SkillConfig::getClassCourseImagePath(getLoginUserId()) . 'thumbs/' . ClassImage::getCoursesPrimaryImage($classname->courses_id) : getWorkNoshopimage(SkillConfig::skill_myinbox_image);
                                $CourseTitle = getClaCourseInfo('Courses', $classname->courses_id);
                                ?>
                                <li class="my-review"><a href="javascript:void(0)"data-toggle="tab" onclick="convertedcourseUser(<?php echo $classname->courses_id; ?>)">
                                        <img src="<?php echo $imageName; ?>" alt=""><div class="name"><?php echo substrclasscourname($CourseTitle->course_titile); ?></div>
                                    </a>
                                </li><!--single-review-->
                                <hr>
                                <?php
                            }


                            $i++;
                        }
                    }
                    ?>
                </ul>        
            </div>

            <div class="col-lg-9 col-md-9 col-sm-9 column9">
                <!--tab content for conversation-->
                <div class="tab-content" id="classCourseConversion">
                    <!--tab pan1 for conversion-->

                    <?php
                    if (!empty($courseId)) {
                        $convertedUser = MakeQuery::getCourseConvertedUser($courseId);
                        $this->renderPartial('//mailConversation/convertedCourseUser', array('dataByUsers' => $convertedUser));
                    }if (!empty($classId)) {
                        $convertedUser = MakeQuery::getClassConvertedUser($classId);
                        $this->renderPartial('//mailConversation/convertedClassUser', array('dataByUsers' => $convertedUser));
                    }
                    ?>

                </div><!--conversion content pan ends-->
            </div>

        </div>
    </div>
</section>
<script>
    function replyAMailToTrainer(classcoursetype, classid, userid, loginuserid, queryId) {
        var subject = $("#subject_id").val();
        var msgval = $("#reply-to-trainer").val();
        if (msgval == '') {
            $("#errorMessage_msg_adminid").text('Message  can not blank.')
            return false;
        }
        if (subject != '' && msgval != '') {
            jQuery.ajax({
                'type': 'POST',
                'url': "<?php echo Yii::app()->createAbsoluteUrl("MailConversation/sendMailTouser"); ?>",
                'data': 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + "&selectedUser=" + userid + "&subject=" + subject + "&msgval=" + msgval + "&classId=" + classid + "&type=" + classcoursetype + "&makeQueryId=" + queryId,
                'beforeSend': function(jqXHR, settings) {
                    $("#conatctTOadminloder").show();
                },
                'success': function(data) {
                    if ($.trim(data) == 'success') {
                        $("#conatctTOadminloder").hide();
                        conatcttoTrainer(loginuserid, userid, classid, classcoursetype);
                    }
                },
                'cache': false
            });
        }

    }



    function replyAMailToUserAdmin() {
        var msgval = $("#reply-to-admin").val();
        if (msgval == '') {
            $("#errorMessage_msg_adminid").text('Message can not blank.')
            return false;
        } else {
            $("#errorMessage_msg_adminid").text('');
        }
        if (msgval != '') {
            jQuery.ajax({
                'type': 'POST',
                'url': "<?php echo Yii::app()->createAbsoluteUrl("ContactAdmin/sendMailTouser"); ?>",
                'data': 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + "&msgval=" + msgval,
                'beforeSend': function(jqXHR, settings) {
                    $("#conatctTOadminloder").show();
                },
                'success': function(data) {
                    if ($.trim(data) == 'success') {
                        $("#conatctTOadminloder").hide();
                        contacttoadmin();
                    }
                },
                'cache': false
            });
        }

    }

    function contacttoadmin() {
        jQuery.ajax({
            'type': 'POST',
            'url': "<?php echo Yii::app()->createAbsoluteUrl("ContactAdmin/conatcttoadmin"); ?>",
            'data': 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val(),
            'success': function(data) {
                $("#replytomailconversion").html('');
                $("#replytomailconversion").html(data);
            },
            'cache': false
        });
    }

    function conatcttoTrainer(fromuserId, touserid, classCourse, type) {
        jQuery.ajax({
            'type': 'POST',
            'url': "<?php echo Yii::app()->createAbsoluteUrl("MailConversation/GetMessage"); ?>",
            data: 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + '&classid=' + classCourse + '&fromuid=' + fromuserId + '&touid=' + touserid + '&type=' + type,
            'success': function(data) {
                $("#replytomailconversion").html('');
                $("#replytomailconversion").html(data);
            },
            'cache': false
        });
    }

    function convertedclassUser(classid) {
        jQuery.ajax({
            'type': 'POST',
            'url': "<?php echo Yii::app()->createAbsoluteUrl("MailConversation/getconverteduserClass"); ?>",
            'data': 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + '&classid=' + classid,
            'success': function(data) {
                $("#classCourseConversion").html('');
                $("#classCourseConversion").append(data);
            },
            'cache': false
        });
    }


    function convertedcourseUser(courseId) {
        jQuery.ajax({
            'type': 'POST',
            'url': "<?php echo Yii::app()->createAbsoluteUrl("MailConversation/getconverteduserCourses"); ?>",
            'data': 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + '&courseId=' + courseId,
            'success': function(data) {
                $("#classCourseConversion").html('');
                $("#classCourseConversion").append(data);
            },
            'cache': false
        });
    }
    function updateConvert(queryId) {
        var checked = 1;
        if ($("#checkbox_covertuser").is(':checked')) {
            jQuery.ajax({
                'type': 'POST',
                'url': "<?php echo Yii::app()->createAbsoluteUrl("MakeQuery/UpdateConvert"); ?>",
                data: 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + "&MakeQuery[id]=" + queryId + '&checked=' + checked,
                'success': function(data) {
                },
                'cache': false});
        }
    }

    function sendAMailToAllConvertedUser(id, type, makeQueryId) {
        var selectedUser = $("#get_converted_users").val();
        var subject = $("#subject_id").val();
        var msgval = $("#message_text_reply").val();
        if (selectedUser == '') {
            $("#error_message").text('');
            $("#error_message").text('Please select at least one user.')
            return false;
        }
        if (subject == '') {
            $("#error_message").text('');
            $("#error_message").text('Subject can not blank.')
        }
        if (msgval == '') {
            $("#error_message").text('');
            $("#error_message").text('Message  can not blank.')
            return false;
        }


        if (subject != '' && msgval != '' && selectedUser != '') {
            jQuery.ajax({
                'type': 'POST',
                'url': "<?php echo Yii::app()->createAbsoluteUrl("MailConversation/sendMailTouser"); ?>",
                'data': 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + "&selectedUser=" + selectedUser + "&subject=" + subject + "&msgval=" + msgval + "&classId=" + id + "&type=" + type + '&makeQueryId=' + makeQueryId,
                'beforeSend': function(jqXHR, settings) {
                    $("#sendnotificationtouser").show();
                },
                'success': function(data) {
                    $("#sendnotificationtouser").hide();
                    if (data == "success") {
                        $("#error_message").text('');
                        $("#error_message").text("Mail Send Successfully .");
                    }
                },
                'cache': false
            });
        }

    }



</script>
